<?php
/**
 * Created by PhpStorm.
 * User: 12133
 * Date: 2024/3/18
 * Time: 10:00
 */

namespace app\controller\admin;
use app\model\admin\Order AS model;
use think\facade\Db;

class Order extends Base
{
    public function list(){
        $where = [];
        $order = model::where($where)
            ->order('order_no DESC')
            ->paginate([
                'page' => $this->page,
                'list_rows' => $this->size
            ]);
        return $this->view('/admin/order/list',['list' => $order]);
    }
    public function info($id = ''){
        $info = Db::name('order')->where('order_no',$id)->find();
        $data['day'] = date('Y-m-d');
        $count = Db::name('order')->where('create_time','>',$data['day'])->count();
        $count = $count + 1001;
        $count = substr($count,1);
        $data['order_no'] = 'LA'.date('Ymd').$count;
        $data['attr'] = Db::name('falan')->field('attr')->group('attr')->select();
        $data['ss'] = Db::name('falan')->field('ss')->group('ss')->select();
        $data['pressure'] = Db::name('falan')->field('pressure')->group('pressure')->select();
        $data['piping'] = Db::name('falan')->field('piping')->group('piping')->select();
        $info['data'] = empty($info['data']) ? [] : json_decode($info['data'],true);
        $my_company = cache('my_company');
        if (!empty($my_company['date'])) {
            $time = 86400 * $my_company['date'];
            $my_company['date'] = date('Y-m-d', $time + strtotime(date('Y-m-d')));
        }
        $my_html = cache('my');
        empty($my_html) && $my_html = [
            ['id' => 'my_name','name' => '报价公司'],
            ['id' => 'my_address','name' => '公司地址'],
            ['id' => 'my_ca','name' => '业务联系人'],
            ['id' => 'my_mobile','name' => '联系方式'],
            ['id' => 'order_no','name' => '报价订单号'],
            ['id' => 'day','name' => '报价日期'],
            ['id' => 'date','name' => '报价有效期'],
            ['id' => 'delivery_date','name' => '交货日期']
        ];
        $company_html = cache('company');
        empty($company_html) && $company_html = [
            ['id' => 'company_name','name' => '客户公司'],
            ['id' => 'address','name' => '公司地址'],
            ['id' => 'nickname','name' => '询价联系人'],
            ['id' => 'mobile','name' => '联系方式']
        ];
        $company_html_top = count($company_html);
        return $this->view('/admin/order/content',[
            'info' => $info,
            'data' => $data,
            'my_company' => $my_company,
            'company_html' => $company_html,
            'my_html' => $my_html,
            'company_html_top' => $company_html_top
        ]);
    }
    public function save(){
        $data = input('post.');
        $data = [
            'order_no' => $data['uniqid'],
            'company_name' => $data['company_name'],
            'end_time' => !empty($data['end_time']) ? $data['end_time'] : date('Y-m-d'),
            'company_address' => $data['address'],
            'delivery_date' => $data['day'],
            'company_contacts' => $data['nickname'],
            'contacts' => '杨天津',
            'name' => '杨天津',
            'contact' => '',
            'quoter' => $data['name'],
            'company_mobile' => $data['mobile'],
            'mobile' => '',
            'auditor' => '杨天津',
            'status' => 1,
            'amount' => $data['amount'] ?? 0,
            'data' => json_encode($data['list'])
        ];
        model::create($data);
        return $this->msg();
    }

    public function create(){
        $post = input('post.');
        $goods_tbody_html = input('goods_tbody_html');
        $update_data = input('update_data');
        $count = Db::name('order')->where('create_time','>', date('Y-m-d'))->count();
        $count = $count + 1001;
        $count = substr($count,1);
        $data = [
            'order_no' => 'LA'.date('Ymd').$count,
            'company_name' => $post['company'],
            'end_time' => !empty($update_data[0]['date']) ? $update_data[0]['date'] : date('Y-m-d'),
            'company_address' => $post['address'],
            'delivery_date' => date('Y-m-d'),
            'company_contacts' => $post['nickname'],
            'contacts' => '杨天津',
            'name' => '杨天津',
            'contact' => '',
            'quoter' => '',
            'company_mobile' => '',
            'mobile' => '',
            'auditor' => '杨天津',
            'status' => 1,
            'amount' => input('amount') ?? 0,
            'data' => json_encode($update_data),
            'html' => $goods_tbody_html
        ];
        model::create($data);
        return $this->msg();
    }

    public function adopt($order_no){
        model::where('order_no',$order_no)->save(['status' => 2]);
        return $this->msg();
    }
    public function refuse($order_no){
        model::where('order_no',$order_no)->save(['status' => 3]);
        return $this->msg();
    }
}